﻿/*----------------------------------------------------------------
// Copyright (C) 2011 上海
// 版权所有。 
//
// 文件名：DataAccessSupplier
// 文件功能描述：供应商管理数据访问层
//
// 
// 创建标识：2012-01-15 王冲
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DbAdapter.sqldbAdapter;
using System.Data.SqlClient;

namespace DAO.WaiGaoQiao
{
    public class DataAccessSupplier
    {
        /// <summary>
        /// 获取供应商信息
        /// </summary>
        /// <returns></returns>
        public DataSet SelSupplierInfo(int pagesize, int startindex, string strquery)
        {
            //sqldb sql = new sqldb();
            //return sql.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, "select * from Con_Instock_Group");
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                int startrow = pagesize * (startindex - 1);
                int endrow = pagesize * startindex + 1;
                string query = "select * from (select ROW_NUMBER() OVER(ORDER BY ID ASC) rownumber,* " +
                               "from Con_Supplier) B "
                             + "where B.rownumber > " + startrow.ToString() + " and B.rownumber < " + endrow.ToString() + strquery;
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        /// <summary>
        /// 供应商数量
        /// </summary>
        /// <returns></returns>
        public int GetSupplierCount()
        {
            sqldb db = new sqldb();
            string query = "select count(1) from Con_Supplier";
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }

        /// <summary>
        /// 保存供应商(新增界面下)
        /// </summary>
        /// <param name="supp"></param>
        /// <returns></returns>
        public int SaveSupplier(object[] supp)
        {
            sqldb db = new sqldb();
            string query = "insert into Con_Supplier(Supplier_NO,Supplier_Name,Contact_Person,Phone,AddRess,Remark,RecordDateTime) Values('" +
                supp[0] + "','" + supp[1] + "','" + supp[2] + "','" + supp[3] + "','" + supp[4] +
                "','" + supp[5] + "',getdate())";
            int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            return rowcount;
        }

        /// <summary>
        /// 保存供应商(修改界面下)
        /// </summary>
        /// <param name="supp"></param>
        /// <returns></returns>
        public int SaveSupplierEdit(object[] supp)
        {
            sqldb db = new sqldb();
            string query = "update Con_Supplier set Supplier_NO='" + supp[1] + "',Supplier_Name='" + supp[2] + "',Contact_Person='" +
                supp[3] + "',Phone='" + supp[4] + "',AddRess='" + supp[5] + "',Remark='" +
                supp[6] + "',RecordDateTime=getdate() where ID=" + supp[0];
            int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            return rowcount;
        }

        /// <summary>
        /// 删除供应商
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public int DeleteSupplier(string id)
        {
            sqldb db = new sqldb();
            string query = "select count(*) from Con_Instock_Detailed where Supplier_ID in(" + id + ")";
            int rowcount = Convert.ToInt32(db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query));
            if (rowcount > 0)
            {
                rowcount = 0;
            }
            else
            {
                query = "Delete from Con_Supplier where ID in(" + id + ")";
                rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            }
            return rowcount;
        }

        /// <summary>
        /// 根据编号获取一条供应商信息
        /// </summary>
        /// <param name="supplierNo"></param>
        /// <returns></returns>
        public DataSet GetSupplierInfoOne(string supplierId)
        {
            sqldb db = new sqldb();
            string query = "select * from Con_Supplier where ID = '" + supplierId + "'";
            return db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
        }
        /// <summary>
        /// 根据ID获取供应商的名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string GetSupplierName(int id)
        {
            sqldb db = new sqldb();
            string query = "select isnull((select Supplier_Name from Con_Supplier where ID = " + id.ToString() + "),'') Supplier_Name";
            object supname = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return supname.ToString();
        }

        public int CheckSupplierNo(string Supplier_NO, string OldSupplier_NO, string AddEdit)
        {
            sqldb db = new sqldb();
            string query = "select count(*) from Con_Supplier where Supplier_NO='" + Supplier_NO + "'";
            if (AddEdit == "Edit")
            {
                query += " and Supplier_NO!='" + OldSupplier_NO + "'";
            }
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }
    }
}
